package net.sppan.base.common.utils;

import net.sppan.base.common.utils.Shiro.ShiroPrincipal;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MingGong on 2017/2/24.
 */
public class ShiroUtils {
    /**
     * 获取当前认证实体的登录名称
     * @return
     */
    public static String getUsername() {
        ShiroPrincipal principal = getPrincipal();
        if(principal != null) return principal.getUsername();
        throw new RuntimeException("user's name is null.");
    }

    /**
     * 获取当前登录的认证实体
     * @return
     */
    public static ShiroPrincipal getPrincipal() {
        Subject subject = SecurityUtils.getSubject();
        return (ShiroPrincipal)subject.getPrincipal();
    }

    /**
     * 获取所有组集合
     * @return
     */
//    public static List<String> getGroups() {
//        List<String> groups = new ArrayList<String>();
//        Integer orgId = getOrgId();
//        ShiroPrincipal principal = getPrincipal();
//        if(principal != null) {
//            groups.addAll(principal.getRoles());
//        }
//        if(orgId != null) {
//            groups.add(String.valueOf(orgId));
//        }
//        return groups;
//    }


}
